package com.rbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbs.entity.Department;
import com.rbs.vo.SalesVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import com.rbs.common.Result;
import java.util.List;

import com.rbs.service.ISalesOrDeptService;
import com.rbs.entity.SalesOrDept;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 业务员表 前端控制器
 * </p>
 *
 * @author lisl
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/sales-or-dept")
public class SalesOrDeptController {

    @Resource
    private ISalesOrDeptService salesOrDeptService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<SalesVO> salesVOList=salesOrDeptService.getAll();
        return Result.success(salesVOList);
    }

    @PostMapping("/saveAndUpdate")
    public Result saveAndUpdate(@RequestBody SalesOrDept salesOrDept) {
        return Result.success(salesOrDeptService.saveOrUpdate(salesOrDept));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(salesOrDeptService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(salesOrDeptService.removeBatchByIds(ids));
    }

    //按名称模糊查询
    //传空值时返回全部
    @GetMapping("findByName")
    public Result findByName(@RequestBody SalesOrDept salesOrDept){
        List<SalesOrDept> sList=salesOrDeptService.findByName(salesOrDept);
        return Result.success(sList);
    }

    //按id排序，true从大到小，false从小到大
    @GetMapping("/orderBy")
    public Result descByID(
            @RequestParam String column,
            @RequestParam boolean descOrAsc,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize){

        if (column.equals("sodStrcode"))column="SOD_strCode";

        QueryWrapper<SalesOrDept> queryWrapper = new QueryWrapper<>();
        if (descOrAsc==true){
            queryWrapper.orderByDesc(column);
        }else if (descOrAsc==false){
            queryWrapper.orderByAsc(column);
        }
        return Result.success(salesOrDeptService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

}

